home *** CD-ROM | disk | FTP | other *** search
/ Kit PC World De Ampliacion De Windows 95 / Kit PC World de ampliacion de Windows 95.iso / internet / sweeper / samples / asyncstg / connect.cpp < prev    next >
C/C++ Source or Header  |  1995-12-03  |  4KB  |  189 lines

  1. #include "connect.h"
  2.  
  3. // IUnknown methods
  4. HRESULT CLockBytesConnectionPoint::QueryInterface(REFIID riid, void** ppObject) 
  5. {
  6.     if (IsBadWritePtr(this, sizeof(*this))) 
  7.     {
  8.         return E_POINTER;
  9.     }
  10.     if (IsBadWritePtr(ppObject, sizeof(*ppObject))) 
  11.     {
  12.         return E_POINTER;
  13.     }
  14.     if (riid==IID_IUnknown || riid==IID_IConnectionPoint) 
  15.     {
  16.         *ppObject=(IConnectionPoint*) this;
  17.     }
  18.     else 
  19.     {
  20.         return E_NOINTERFACE;
  21.     }
  22.     AddRef();
  23.     return S_OK;
  24. }
  25.  
  26. ULONG CLockBytesConnectionPoint::AddRef() 
  27. {
  28.     if (IsBadWritePtr(this, sizeof(*this))) 
  29.     {
  30.         return E_POINTER;
  31.     }
  32.     InterlockedIncrement((long*) &g_dwRefCount);
  33.     InterlockedIncrement((long*) &m_dwRefCount);
  34.     return m_dwRefCount;
  35. }
  36.  
  37. ULONG CLockBytesConnectionPoint::Release() 
  38. {
  39.     if (IsBadWritePtr(this, sizeof(*this))) 
  40.     {
  41.         return E_POINTER;
  42.     }
  43.     ULONG dwRefCount=m_dwRefCount-1;
  44.     InterlockedDecrement((long*) &g_dwRefCount);
  45.     if (InterlockedDecrement((long*) &m_dwRefCount)==0) 
  46.     {
  47.         delete this;
  48.         return 0;
  49.     }
  50.     return dwRefCount;
  51. }
  52.  
  53. // IConnectionPoint methods
  54. HRESULT CLockBytesConnectionPoint::GetConnectionInterface(IID FAR* pIID) 
  55. {
  56.     if (IsBadWritePtr(this, sizeof(*this))) 
  57.     {
  58.         return E_POINTER;
  59.     }
  60.     if (IsBadWritePtr(pIID, sizeof(*pIID))) 
  61.     {
  62.         return E_POINTER;
  63.     }
  64.     *pIID=IID_ILockBytes;
  65.     return S_OK;
  66. }
  67.  
  68. HRESULT CLockBytesConnectionPoint::GetConnectionPointContainer(IConnectionPointContainer FAR* FAR* ppCPC) 
  69. {
  70.     if (IsBadWritePtr(this, sizeof(*this))) 
  71.     {
  72.         return E_POINTER;
  73.     }
  74.     if (IsBadWritePtr(ppCPC, sizeof(*ppCPC))) 
  75.     {
  76.         return E_POINTER;
  77.     }
  78.     if (IsBadWritePtr(m_pBA, sizeof(*m_pBA)))
  79.     {
  80.         return E_UNEXPECTED;
  81.     }
  82.     if (FAILED(m_pBA->QueryInterface(IID_IConnectionPointContainer, (void**) ppCPC))) 
  83.     {
  84.         return E_UNEXPECTED;
  85.     }
  86.     return S_OK;
  87. }
  88.  
  89. HRESULT CLockBytesConnectionPoint::Advise(LPUNKNOWN pUnkSink, DWORD FAR* pdwCookie) 
  90. {
  91.     if (IsBadWritePtr(this, sizeof(*this))) 
  92.     {
  93.         return E_POINTER;
  94.     }
  95.     if (IsBadWritePtr(pUnkSink, sizeof(*pUnkSink))) 
  96.     {
  97.         return E_POINTER;
  98.     }
  99.     if (IsBadWritePtr(pdwCookie, sizeof(*pdwCookie))) 
  100.     {
  101.         return E_POINTER;
  102.     }
  103.     if (IsBadWritePtr(m_pBA, sizeof(*m_pBA)))
  104.     {
  105.         return E_UNEXPECTED;
  106.     }
  107.     if (NULL!=m_pBA->m_pCacheArray && NULL!=m_pBA->m_pDataArray) 
  108.     {
  109.         return CONNECT_E_ADVISELIMIT;
  110.     }
  111.     ILockBytes* pLB=NULL;
  112.     if (FAILED(pUnkSink->QueryInterface(IID_ILockBytes, (void**) &pLB))) 
  113.     {
  114.         return CONNECT_E_CANNOTCONNECT;
  115.     }
  116.     HRESULT hRes=E_UNEXPECTED;
  117.     EnterCriticalSection(&m_pBA->m_sectLBConnect);
  118.     if (NULL==m_pBA->m_pCacheArray) 
  119.     {
  120.         m_pBA->m_pCacheArray=pLB;
  121.         *pdwCookie=1;
  122.         hRes=S_OK;
  123.     }
  124.     else if (NULL==m_pBA->m_pDataArray) 
  125.     {
  126.         m_pBA->m_pDataArray=pLB;
  127.         *pdwCookie=2;
  128.         hRes=S_OK;
  129.     }
  130.     LeaveCriticalSection(&m_pBA->m_sectLBConnect);
  131.     if (FAILED(hRes))
  132.     {
  133.         pLB->Release();
  134.     }
  135.     return hRes;
  136. }
  137.  
  138. HRESULT CLockBytesConnectionPoint::Unadvise (DWORD dwCookie) 
  139. {
  140.     if (IsBadWritePtr(this, sizeof(*this))) 
  141.     {
  142.         return E_POINTER;
  143.     }
  144.     if (IsBadWritePtr(m_pBA, sizeof(*m_pBA)))
  145.     {
  146.         return E_UNEXPECTED;
  147.     }
  148.     HRESULT hRes=CONNECT_E_NOCONNECTION;
  149.     if (dwCookie==1 || dwCookie==2) 
  150.     {
  151.         EnterCriticalSection(&m_pBA->m_sectLBConnect);
  152.         if (dwCookie==1) 
  153.         {
  154.             if (NULL!=m_pBA->m_pCacheArray) 
  155.             {
  156.                 m_pBA->m_pCacheArray->Release();
  157.                 m_pBA->m_pCacheArray=NULL;
  158.                 hRes=S_OK;
  159.             }
  160.         } 
  161.         else if (dwCookie==2) 
  162.         {
  163.             if (NULL!=m_pBA->m_pDataArray)
  164.             {
  165.                 m_pBA->m_pDataArray->Release();
  166.                 m_pBA->m_pDataArray=NULL;
  167.                 hRes=S_OK;
  168.             }
  169.         }
  170.         LeaveCriticalSection(&m_pBA->m_sectLBConnect);
  171.     }
  172.     return hRes;
  173. }
  174.  
  175. HRESULT CLockBytesConnectionPoint::EnumConnections(LPENUMCONNECTIONS FAR* ppEnum) {
  176.     return E_NOTIMPL;
  177. }
  178.  
  179. CLockBytesConnectionPoint::CLockBytesConnectionPoint(CAsyncByteArray* pBA)
  180. {
  181.     m_dwRefCount=0;
  182.     m_pBA=pBA;
  183. }
  184.  
  185. CLockBytesConnectionPoint::~CLockBytesConnectionPoint()
  186. {
  187.     m_pBA->m_pLBConnect=NULL;
  188. }
  189.